#include "case.h"
QMap<QString,QPixmap> Case::images = QMap<QString,QPixmap>();


Case::Case(int x)
{
    if (images.empty())
    {
        images["herbe"] = QPixmap("Ressources/herbe.jpg");
        images["cheminest"] = QPixmap("Ressources/cheminest.jpg");
        images["cheminsudest"] = QPixmap("Ressources/cheminsudest.jpg");
        images["boue"] = QPixmap("Ressources/boue.jpg");
        images["but2"] = QPixmap("Ressources/but2.jpg");
        images["trou"] = QPixmap("Ressources/trou.jpg");
    }
    rotation = 0;
    herbe = false;
    nord = x & 1;
    sud = x & 2;
    est = x & 4;
    ouest = x & 8;
    depart = x & 16;
    arrivee = x & 32;
    boue = x & 64;
    int nbdir = 0;
    if (nord) nbdir++;
    if (sud) nbdir++;
    if (est) nbdir++;
    if (ouest) nbdir++;
    if (nbdir > 3) throw "Erreur : 3 directions";
    if ((nord && sud) || (est && ouest)) throw "Erreur : 2 directions opposées";

    if (boue) setPixmap(images["boue"]);
    else if (arrivee) setPixmap(images["but2"]);
    else if (depart) setPixmap(images["trou"]);
    else if (boue) setPixmap(images["boue"]);
    else if (nbdir == 1)
    {
        if (depart) setPixmap(images["trou"]);
        else setPixmap(images["cheminest"]);
        if (est) rotation = 0;
        else if (sud) rotation = 90;
        else if (ouest) rotation = 180;
        else rotation = 270;
        setRotation(rotation);
    }
    else if (nbdir == 2)
    {
        if (depart) setPixmap(images["trou"]);
        else setPixmap(images["cheminsudest"]);
        if (est && sud) rotation = 45;
        else if (sud && ouest) rotation = 135;
        else if (nord && ouest) rotation = 225;
        else rotation = 315;
        setRotation(rotation-45);
    }
    else
    {
        setPixmap(images["herbe"]);
        herbe = true;
    }
}
